<?php

/*
 * Yanis Framework, Yet ANother Internet Service for web and mobile technologists
 * @copyright Copyright (C) 2011 ch@rlesen. All rights reserved.
 * @license GNU/GPL
 * @author Charles EDOU NZE <charles at charlesen.fr>
 */

/**
 * Class Users
 *
 */
class User {

    /**
     * @var Object The Registry object 
     */
    private $registry;

    /**
     * @var String The Users table name 
     */
    private $users_tablename = 'users';

    /**
     * @var int The user ID 
     */
    private $id;

    /**
     * @var String The Username 
     */
    private $username;

    /**
     * @var String User email 
     */
    private $email;

    /**
     * @var Bool
     */
    private $active;

    /**
     * @var int (0 or 1) 
     */
    private $banned;

    /**
     * @var int 
     */
    private $usertype;

    /**
     * @var String 
     */
    private $pwd_resey_key;

    /**
     * @var Bool
     */
    private $valid;

    /**
     * The constructor takes 4 arguments, the registry (dependency injection, so
     * it can communicate with the rest of the framework), a user ID, a username, and 
     * a password.
     * 
     * @param Registry $registry
     * @param type $id
     * @param type $username
     * @param type $password 
     */
    public function __construct(Registry $registry, $id=0, $username='', $password='') {
        $this->registry = $registry;

        /**
         * If we haven't set a user ID ($id=0) and we have set a username and a password,
         * we should look up the user to see whether these are valid credentials
         */
        if ($id == 0 && $username != '' && $password != '') {
            $user = $this->registry->getObject('db')->sanitizedData($username);
            $hash = md5($password);
            $sql = "SELECT * FROM " . $this->users_tablename . "WHERE username ='{$user}' AND 
            password='{$hash}' AND deleted=0";
            $this->registry->getObject('db')->executeQuery($sql);

            if ($this->registry->getObject('db')->numRows() == 1) {
                $data = $this->registry->getObject('db')->getRows();

                $this->id = $data['id'];
                $this->username = $data['username'];
                $this->email = $data['email'];
                $this->active = $data['active'];
                $this->banned = $data['banned'];
                $this->usertype = $data['usertype'];
                $this->pwd_resey_key = $data['pwd_reset_key'];
                $this->valid = true;
            } elseif ($id > 0) {
                $id = intval($id);
                $sql = "SELECT * FROM" . $this->users_tablename . "WHERE id='{$id}' AND deleted=0";
                $this->registry->getObject('db')->executeQuery($sql);
                if ($this->registry->getObject('db')->numRows() == 1) {
                    $data = $this->registry->getObject('db')->getRows();

                    $this->id = $data['id'];
                    $this->username = $data['username'];
                    $this->email = $data['email'];
                    $this->active = $data['active'];
                    $this->banned = $data['banned'];
                    $this->usertype = $data['usertype'];
                    $this->pwd_resey_key = $data['pwd_reset_key'];
                    $this->valid = true;
                }
            }
        }
    }

}

?>
